package com.blue.rpc.core.codec;

import com.blue.rpc.core.message.MessageProtostuffConvertor;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * Protostuff消息编码器
 * 
 * @author zhengj
 * @since 1.0 2017年3月13日
 */
public class MessageProtostuffEncoder extends MessageToByteEncoder<Object>
{
	public MessageProtostuffEncoder()
	{
	}

	@Override
	protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception
	{
		byte[] bytes = MessageProtostuffConvertor.toBytes(msg);
		out.writeInt(bytes.length);
		out.writeBytes(bytes);
	}

}
